//********************************************************************
// 
//  file:  PurchasingManagementSample.cs
//  PurchasingManagement Sample Call
//
//  (C) Copyright 2010, Diomede Corporation
//  All rights reserved
// 
//  Use, modification, and distribution is subject to   
//  the New BSD License (See accompanying file license.txt).
// 
//********************************************************************

using System;
using data.diomedestorage.com;
using Diomede.SDK.Enums;

namespace Diomede.SDK.Example.Samples
{
    class PurchasingManagementSample
    {
        static string username = string.Format("user_{0}", DateTime.Now.Ticks);
        static string password = "password914";
        static string email = string.Format("{0}@example.com", username);

        public void Start()
        {
            try
            {
                CreateUser();

                Console.WriteLine("\n\n");
                GetAllProductsSample();
                PuchaseProductsSample();
                GetMyProductsSample();
                CancelProductsSample();
                GetAllContractsSample();
                PuchaseContractsSample();
                GetMyContractsSample();
                CancelContractsSample();
                //GetAllSupportsSample();
                //PuchaseSupportsSample();
                //GetMySupportsSample();
                //CancelSupportsSample();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Utils.WriteMessage("Press any key...");
            Console.ReadKey();

        }

        private static void CreateUser()
        {
            ISession session = new Session();
            IAccountManagement accountManager = session as IAccountManagement;
            if (accountManager.Create(username, password, email).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to create new user.");
            }
        }

        private static void LoginSampleUser(ISession session)
        {
            if (session.Login(username, password).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to login.");
            }
        }

        #region Examples

        #region Products
        public void GetAllProductsSample()
        {
            ISession session = new Session();
            IPurchasingManagement pm = session as IPurchasingManagement;
            ProductsExecResult result = pm.GetAllProducts();

            if (result.Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to get all products.");
            }
            else
            {
                Console.WriteLine("All products successfully retrieved.");
            }

        }

        public void PuchaseProductsSample()
        {
            ISession session = new Session();

            LoginSampleUser(session);

            IPurchasingManagement pm = session as IPurchasingManagement;
            ProductsExecResult getproducts = pm.GetAllProducts();

            Product productToPurchase = getproducts.Products[0];

            if (pm.PurchaseProduct(productToPurchase.productID).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to purchase products.");
            }
            else
            {
                Console.WriteLine("Product succesfully purchased.");
            }

        }

        public void GetMyProductsSample()
        {
            ISession session = new Session();

            LoginSampleUser(session);

            IPurchasingManagement pm = session as IPurchasingManagement;
            ProductsExecResult getproducts = pm.GetAllProducts();

            Product productToPurchase = getproducts.Products[0];

            ProductsExecResult purchasePrudResult = pm.PurchaseProduct(productToPurchase.productID);

            if (pm.GetMyProducts().Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to retrieve my products.");
            }
            else
            {
                Console.WriteLine("My Product successfully retrieved.");
            }
        }

        public void CancelProductsSample()
        {
            ISession session = new Session();

            LoginSampleUser(session);

            IPurchasingManagement pm = session as IPurchasingManagement;
            ProductsExecResult getproducts = pm.GetAllProducts();

            Product productToPurchase = getproducts.Products[0];

            ProductsExecResult purchasePrudResult = pm.PurchaseProduct(productToPurchase.productID);

            if (pm.CancelProduct(pm.GetMyProducts().UserProducts[0].product.productID).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to cancel products.");
            }
            else
            {
                Console.WriteLine("My Product successfully cancelled.");
            }

        }

        #endregion

        #region Contracts
        public void GetAllContractsSample()
        {
            ISession session = new Session();
            IPurchasingManagement pm = session as IPurchasingManagement;
            ContractsExecResult result = pm.GetAllContracts();

            if (result.Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to get all Contracts.");
            }
            else
            {
                Console.WriteLine("All Contracts successfully retrieved.");
            }

        }

        public void PuchaseContractsSample()
        {
            ISession session = new Session();

            LoginSampleUser(session);

            IPurchasingManagement pm = session as IPurchasingManagement;
            ContractsExecResult getContracts = pm.GetAllContracts();

            Contract contractToPurchase = getContracts.Contracts[0];

            if (pm.PurchaseContract(contractToPurchase.contractID).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to purchase Contracts.");
            }
            else
            {
                Console.WriteLine("contract succesfully purchased.");
            }

        }

        public void GetMyContractsSample()
        {
            ISession session = new Session();

            LoginSampleUser(session);

            IPurchasingManagement pm = session as IPurchasingManagement;
            ContractsExecResult getContracts = pm.GetAllContracts();

            Contract contractToPurchase = getContracts.Contracts[0];

            ContractsExecResult purchasePrudResult = pm.PurchaseContract(contractToPurchase.contractID);

            if (pm.GetMyContracts().Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to retrieve my Contracts.");
            }
            else
            {
                Console.WriteLine("My contract successfully retrieved.");
            }
        }

        public void CancelContractsSample()
        {
            ISession session = new Session();

            LoginSampleUser(session);

            IPurchasingManagement pm = session as IPurchasingManagement;
            ContractsExecResult getContracts = pm.GetAllContracts();

            Contract contractToPurchase = getContracts.Contracts[0];

            ContractsExecResult purchasePrudResult = pm.PurchaseContract(contractToPurchase.contractID);

            if (pm.CancelContract(pm.GetMyContracts().UserContracts[0].userContractID).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to cancel Contracts.");
            }
            else
            {
                Console.WriteLine("My contract successfully cancelled.");
            }

        }

        #endregion

        #region Supports
        //public void GetAllSupportsSample()
        //{
        //    ISession session = new Session();
        //    IPurchasingManagement pm = session as IPurchasingManagement;
        //    SupportsExecResult result = pm.GetAllSupports();

        //    if (result.Status != ExecutionResults.Success)
        //    {
        //        throw new Exception("Fails to get all Supports.");
        //    }
        //    else
        //    {
        //        Console.WriteLine("All Supports successfully retrieved.");
        //    }

        //}

        //public void PuchaseSupportsSample()
        //{
        //    ISession session = new Session();

        //    LoginSampleUser(session);

        //    IPurchasingManagement pm = session as IPurchasingManagement;
        //    SupportsExecResult getSupports = pm.GetAllSupports();

        //    Support supportToPurchase = getSupports.Supports[0];

        //    if (pm.PurchaseContract(supportToPurchase.supportID).Status != ExecutionResults.Success)
        //    {
        //        throw new Exception("Fails to purchase Supports.");
        //    }
        //    else
        //    {
        //        Console.WriteLine("contract succesfully purchased.");
        //    }

        //}

        //public void GetMySupportsSample()
        //{
        //    ISession session = new Session();

        //    LoginSampleUser(session);

        //    IPurchasingManagement pm = session as IPurchasingManagement;
        //    SupportsExecResult getSupports = pm.GetAllSupports();

        //    Support supportToPurchase = getSupports.Supports[0];

        //    SupportsExecResult purchasePrudResult = pm.PurchaseSupport(supportToPurchase.supportID);

        //    if (pm.GetMySupports().Status != ExecutionResults.Success)
        //    {
        //        throw new Exception("Fails to retrieve my Supports.");
        //    }
        //    else
        //    {
        //        Console.WriteLine("My contract successfully retrieved.");
        //    }
        //}

        //public void CancelSupportsSample()
        //{
        //    ISession session = new Session();

        //    LoginSampleUser(session);

        //    IPurchasingManagement pm = session as IPurchasingManagement;
        //    SupportsExecResult getSupports = pm.GetAllSupports();

        //    Support supportToPurchase = getSupports.Supports[0];

        //    SupportsExecResult purchasePrudResult = pm.PurchaseSupport(supportToPurchase.supportID);

        //    if (pm.CancelContract(pm.GetMySupports().UserSupports[0].userSupportID).Status != ExecutionResults.Success)
        //    {
        //        throw new Exception("Fails to cancel Supports.");
        //    }
        //    else
        //    {
        //        Console.WriteLine("My contract successfully cancelled.");
        //    }

        //}

        #endregion

        #endregion
    }
}
